package com.rss;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListaArticulo extends ListActivity {

	private List<Articulo> articulos;
	private Feed feed;
	private RssDB db;
	private static final int ACTIVITY_LEIDO = 5;
	private static final int ACTIVITY_LEIDO_TODO = 6;

	@Override
	protected void onCreate(Bundle icicle) {
		try {
			super.onCreate(icicle);
			db = new RssDB(this);
			Window w = getWindow();
			w.requestFeature(Window.FEATURE_LEFT_ICON);
			setContentView(R.layout.lista_articulos);
			w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
					R.drawable.icon);

			feed = new Feed();

			if (icicle != null) {
				feed.feedId = icicle.getLong("feed_id");
				feed.titulo = icicle.getString("titulo");
				feed.url = new URL(icicle.getString("url"));
			} else {
				Bundle extras = getIntent().getExtras();
				feed.feedId = extras.getLong("feed_id");
				feed.titulo = extras.getString("titulo");
				feed.url = new URL(extras.getString("url"));

				db.open();
				db.borrarArticulos(feed.feedId);
				db.close();
				RssManejador rh = new RssManejador();
				rh.actualizarArticulos(this, feed);
			}

			fillData();

		} catch (MalformedURLException e) {
			Log.e("RSSREADER Lista articulo oncreate", e.toString());
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong("feed_id", feed.feedId);
		outState.putString("titulo", feed.titulo);
		outState.putString("url", feed.url.toString());
	}

	@Override
	// TODO : revisar el parse
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		// TODO paso actividad a otra
		Articulo art = articulos.get(position);

		Intent i = new Intent(this, muestraArticulo.class);
		i.putExtra("html", art.contenido);
		// TODO marcar como leido=1
		db.open();
		db.marcaArticuloLeido(art.articuloId);
		db.close();
		startActivity(i);
	}

	private void fillData() {
		List<String> items = new ArrayList<String>();
		db.open();
		articulos = db.getArticulos(feed.feedId);
		db.close();
		for (Articulo articulo : articulos) {
			if (articulo.leido == 0)
				items.add(articulo.titulo);
		}

		ArrayAdapter<String> notes = new ArrayAdapter<String>(this,
				R.layout.fila_articulo, items);
		setProgressBarVisibility(false);

		setListAdapter(notes);

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, ACTIVITY_LEIDO, 0, R.string.marcarLeido);
		menu.add(0, ACTIVITY_LEIDO_TODO, 0, R.string.marcarTodoLeido);

		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case ACTIVITY_LEIDO:
			db.open();
			Articulo ar = articulos.get(getSelectedItemPosition());
			db.marcaArticuloLeido(ar.articuloId);
			db.close();
			break;

		case ACTIVITY_LEIDO_TODO:
			db.open();
			db.marcaTodosArticuloLeido(feed.feedId);
			db.close();
			break;
		}

		return true;
	}

}
